using System.Collections.Generic;
using System.IO;
using Common;
using Common.Enum;

namespace SExperiment
{
	public class ExperimentTemporary
	{
		private EProjectType _Type = EProjectType.All;

		private EExperimentStatus _Status;

		private string _BreakPoint;

		public EProjectType Type
		{
			get
			{
				return _Type;
			}
			set
			{
				_Type = value;
			}
		}

		public EExperimentStatus Status
		{
			get
			{
				return _Status;
			}
			set
			{
				_Status = value;
			}
		}

		public string BreakPoint
		{
			get
			{
				return _BreakPoint;
			}
			set
			{
				_BreakPoint = value;
			}
		}

		public virtual void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_ExperimentTemporaryInfo);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			list.Add(PropertyKey.Key_Type);
			list2.Add(BytesConverter.GetBytes((int)Type));
			list.Add(PropertyKey.Key_Status);
			list2.Add(BytesConverter.GetBytes((int)Status));
			if (!string.IsNullOrEmpty(BreakPoint))
			{
				list.Add(PropertyKey.Key_RefTargets);
				list2.Add(BytesConverter.GetBytes(BreakPoint));
			}
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public virtual bool ReadFromStream(StreamEx stream)
		{
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_ExperimentTemporaryInfo)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					for (int i = 0; i < keys.Count; i++)
					{
						switch (keys[i])
						{
						case PropertyKey.Key_Type:
							Type = (EProjectType)BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_Status:
							Status = (EExperimentStatus)BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_RefTargets:
							BreakPoint = BytesConverter.BytesToString(dates[i]);
							break;
						}
					}
				}
				else
				{
					stream.Seek(-4L, SeekOrigin.Current);
					int len = stream.ReadInt();
					StreamEx streamEx = new StreamEx();
					streamEx.Write(stream.ReadBytes(len));
					streamEx.Seek(0L, SeekOrigin.Begin);
					if (streamEx.ReadKey() == PropertyKey.Key_Index)
					{
						List<PropertyKey> keys2;
						List<byte[]> dates2;
						streamEx.ReadIndex(out keys2, out dates2);
						Status = (EExperimentStatus)BytesConverter.BytesToInt(dates2[0]);
						BreakPoint = BytesConverter.BytesToString(dates2[1]);
					}
					else
					{
						Status = (EExperimentStatus)streamEx.ReadKey();
						if (Status == EExperimentStatus.Running)
						{
							BreakPoint = streamEx.ReadString();
						}
					}
				}
				return true;
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
